using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MyHunter.Interfaces;

namespace MyHunter.Tools
{
    public delegate void GoToShot(DataSchema.CollectionSchema.ShotRow sr);
    public partial class Find : Form
    {
        #region MEMBERS
        MyHunter.Interfaces.ICollectionManager CollMgr;
        public event GoToShot GoToShotEvent;
        DataSchema.CollectionSchema.ShotRow[] FoundRows = null;
        List<string> FoundItems;
        #endregion

        #region CTOR
        public Find(MyHunter.Interfaces.ICollectionManager col)
        {
            InitializeComponent();
            CollMgr = col;
            FoundItems = new List<string>();
        }
        #endregion

        #region EVENTS
        private void buttonFind_Click(object sender, EventArgs e)
        {
            string sql = this.BuildQuery();
            
            FoundItems.Clear();

            if (sql == "")
            {
                MessageBox.Show("Enter any criteria");
                return;
            }

            try
            {
                SearchIn si = this.radioButtonCurrent.Checked? SearchIn.Current:(this.radioButtonLoaded.Checked? SearchIn.Loaded: SearchIn.MyCollections);
                
                FoundRows = CollMgr.FindShot(sql, si, FoundItems, this.checkBoxLink.Checked);

                this.shotDataTableBindingSource.DataSource = FoundRows;
            }
            catch(Exception exc)
            {
                throw;
            }
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string colpath = DataSchema.Settings.SettingsMgr.DefaultCollectionsPath;
                Engine.Factory.CollectionFactory.ExportToEntity(saveFileDialog.FileName, this.FoundItems);
            }
        }
        #endregion

        #region PRIVATE
        private void GoTo(object sender, DataGridViewCellEventArgs e)
        {
            DataSchema.CollectionSchema.ShotRow ir =
                (DataSchema.CollectionSchema.ShotRow)
                (this.dataGridViewShot.Rows[e.RowIndex].DataBoundItem);
            if (GoToShotEvent != null)
                GoToShotEvent(ir);
        }

        private string BuildQuery()
        {
            string sql = "";

            if (this.textBoxFind.Text != "")
                sql += "Name like '%" + this.textBoxFind.Text + "%'";
            if (this.numericUpDownRank.Value != 0)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " Rank = " + numericUpDownRank.Value.ToString();
            }

            if (this.checkBoxAT.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " AT = true";
            }

            if (this.checkBoxAS.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " AS = true";
            }


            if (this.checkBoxDP.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " DP = true";
            }

            if (this.checkBoxST.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " ST = true";
            }

            if (this.checkBoxHB.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " HB = true";
            }

            if (this.checkBoxHL.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " HL = true";
            }

            if (this.checkBoxHS.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " HS = true";
            }

            if (this.checkBoxSB.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " SB = true";
            }

            if (this.checkBoxSL.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " SL = true";
            }

            if (this.checkBoxSS.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " SS = true";
            }

            if (this.checkBoxFM.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " FM = true";
            }

            if (this.checkBoxPS.CheckState == CheckState.Checked)
            {
                if (sql != "")
                    sql += " AND ";
                sql += " PS = true";
            }

            if ( this.radioButtonPictures.Checked )
            {
                if (sql != "")
                    sql += " AND ";
                sql += " Pics = true";
            }

            if ( this.radioButtonVideo.Checked )
            {
                if (sql != "")
                    sql += " AND ";
                sql += " Pics = false";
            }





            return sql;
        }
        #endregion


    }
}